﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gmis.Public;
using System.IO;

namespace Gmis.Web.Manage.Party
{
    /// <summary>
    /// PMember 的摘要说明
    /// </summary>
    public class PMemberHandler : IHttpHandler
    {
  //            HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
  //          fileLength = upFile.ContentLength;//记录文件的长度
  // try
  // {
  //  if(fileLength==0)//当文件长度为0的时候
  //  {
  //   txtMessage.Text = "请选择要上传的文件!";
  //  }
  //  else
  //  {
  //   byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
  //   Stream fileStream = upFile.InputStream;//建立文件流对象
  //   fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
  //   string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
  //   SqlConnection conn = new SqlConnection(connString);//初始化数据库连接
  //   string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)";
  //   //插入数据库语句
  //   SqlCommand comm = new SqlCommand(insertStr,conn);
  //   comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数
  //   comm.Parameters["@image_data"].Value = fileByte;//给参数赋值
  //   comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50));
  //   comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型
  //   comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50));
  //   comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传
  //   comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4));
  //   comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用
  //   conn.Open();//打开数据库连接
  //   comm.ExecuteNonQuery();//添加数据
  //   conn.Close();//关闭数据库
  //   txtMessage.Text = "你已经成功的上传了图片";
  //  }
  // }
  // catch(Exception ex)
  // {
  //     txtMessage.Text = ex.Message.ToString();
  // }
  //}
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string sId = context.Request["id"];
            string action = context.Request["action"];
            //object ojb = context.Request["Fud_Pic"];
            //foreach (HttpPostedFile postedFile in context.Request.Files)
            //{
            //    int contentLength = postedFile.ContentLength;
            //    string contentType = postedFile.ContentType;
            //    string fileName = postedFile.FileName;

            //    //postedFile.Save(@"c:\test" + index + ".tmp");

            //    //index++;
            //}

            //HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
            //fileLength = upFile.ContentLength;//记录文件的长度
            //
            if (string.IsNullOrEmpty(action))
            {
                context.Response.Write(CommonFunction.failure("无指定操作"));
            }
            else
            {
                switch (action.ToLower())
                {
                    case "add":
                        context.Response.Write(add());
                        break;
                    case "delete":
                        context.Response.Write(delete(sId));
                        break;
                    case "deleteall":
                        context.Response.Write(deleteAll(context.Request["ids"]));
                        break;
                    case "edit":
                        context.Response.Write(update());
                        break;
                }
            }
        }
        private Model.PMember GenerateModel()
        {
            //获取form数据
            Model.PMember model = new Model.PMember();
            model.sId = CommonFunction.getRequestValue("txt_sId");
            if (model.sId == "") model.sId = CommonFunction.GetGuid();//主键
            model.sName = CommonFunction.getRequestValue("txt_sName");//姓名 context.Request["txt_sName"];     
            model.sNumber = CommonFunction.getRequestValue("txt_sNumber");//编号
            model.sSex = CommonFunction.getRequestValue("cbx_sSex");//性别
            model.sIdentity = CommonFunction.getRequestValue("txt_sIdentity");//身份证号
            model.dBorn = Convert.ToDateTime(CommonFunction.GetBrithdayFromIdCard(model.sIdentity));//出生日期
            model.nAge = DateTime.Now.Year - model.dBorn.Year;//年龄
            model.sMarry = CommonFunction.getRequestValue("cbx_sMarry");//婚姻状况
            model.sNation = CommonFunction.getRequestValue("cbx_sNation");//民族
            model.sEducation = CommonFunction.getRequestValue("cbx_sEducation");//学历
            model.sWork = CommonFunction.getRequestValue("txt_sWork");//职业
            model.sWorkPlace = CommonFunction.getRequestValue("txt_sWorkPlace");//工作单位
            model.sPosition = CommonFunction.getRequestValue("txt_sPosition");//职务
            model.sNative = CommonFunction.getRequestValue("txt_sNative");//籍贯
            model.dJoin = Convert.ToDateTime(CommonFunction.getRequestValue("txt_dJoin"));//入党日期
            model.sMobile = CommonFunction.getRequestValue("txt_sMobile");//手机号码
            model.sAddress = CommonFunction.getRequestValue("txt_sAddress");//家庭住址
            model.sPicture = Path.GetFileName(CommonFunction.getRequestValue("txt_sPicture"));//图片路径
            model.sCharge = CommonFunction.getRequestValue("txt_sCharge");//党费收缴情况
            model.sComment = "";
            model.iPicture = null;
            //流入时间，流入地，流入原因
            model.dInDate = CommonFunction.varToDateTimeDef(CommonFunction.getRequestValue("txt_sInDate"), null);
            model.sInPlace = CommonFunction.getRequestValue("txt_sInPlace");
            model.sInReason = CommonFunction.getRequestValue("txt_sInReason");
            //流出时间，流出地，流出原因
            model.dOutDate = CommonFunction.varToDateTimeDef(CommonFunction.getRequestValue("txt_sOutDate"), null);
            model.sOutPlace = CommonFunction.getRequestValue("txt_sOutPlace");
            model.sOutReason = CommonFunction.getRequestValue("txt_sOutReason");
            return model;
        }
        //增加一条
        private string add()
        {
            BLL.PMember bll = new BLL.PMember();
            if (bll.Add(GenerateModel()))
            {
                return CommonFunction.successAndCloseCurrent("", "", "PMember");
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除一条
        private string delete(string sId)
        {
            BLL.PMember bll = new BLL.PMember();
            if (bll.Delete(sId))
            {
                return CommonFunction.success();
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除全部
        private string deleteAll(string ids)
        {
            string[] strs = ids.Split(',');
            try
            {
                BLL.PMember bll = new BLL.PMember();
                foreach (string str in strs)
                {
                    bll.Delete(str);
                }
                return CommonFunction.success();
            }
            catch { return CommonFunction.failure(); }


        }
        //更新数据
        private string update()
        {
            BLL.PMember bll = new BLL.PMember();
            if (bll.Update(GenerateModel()))
            {
                return CommonFunction.successAndCloseCurrent("", "", "PMember");
            }
            else
            {
                return CommonFunction.failure();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}